package fast.mvc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class RemoteReforge {

	private StringBuilder result = new StringBuilder();
	
	public RemoteReforge(String json){
		try {
			URL u = new URL("http://shadowref.appspot.com/calc");
			HttpURLConnection connection = (HttpURLConnection) u.openConnection();
			connection.setRequestMethod( "POST" );
			connection.setDoInput( true );
			connection.setDoOutput( true );
			connection.setUseCaches( false );
			
			OutputStreamWriter writer = new OutputStreamWriter( connection.getOutputStream() );
			writer.write( json );
			writer.flush();


			BufferedReader reader = new BufferedReader(
			                          new InputStreamReader(connection.getInputStream()) );

			for ( String line; (line = reader.readLine()) != null; )
			{
			  System.out.println( line );
			  result.append(line);
			}

			writer.close();
			reader.close();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getResult() {
		return result.toString();
	}

}
